package cn.power.win.win_power.fragment;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.io.Serializable;
import java.math.BigDecimal;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


import butterknife.BindView;
import cn.power.win.win_power.MyApp;
import cn.power.win.win_power.R;

import cn.power.win.win_power.utils.DeviceBean;
import cn.power.win.win_power.utils.MessageEvent;
import cn.power.win.win_power.utils.RxBus;
import cn.power.win.win_power.utils.SettingEvent;
import cn.power.win.win_power.utils.SettingEvent2;
import cn.power.win.win_power.utils.SettingEvent3;
import cn.power.win.win_power.utils.SpUtils;
import cn.power.win.win_power.view.CircleRangeView;
import me.jessyan.autosize.internal.CustomAdapt;

import static android.content.ContentValues.TAG;


/**
 * Created by Jay on 2019/1/28 0028.
 * 实时界面
 */
public class ActualTimeFragment extends Fragment implements CustomAdapt {
    //风机参数布局
    @BindView(R.id.ll_fan)
    LinearLayout llFan;
    //累计发电
    @BindView(R.id.ll_total_power)
    LinearLayout llTotalPower;
    //  ----------------------------  GPRS 通信部分  ------------------------------
    private TextView mTxt_content;
    private String mID;                           // 从NodeFragment中传过来的参数
    private String old_ID = null;
    // private String mId;                        // 获取保存的id

    private Socket mmSocket;                      // 新建套接字
    private InputStream mmInStream;               // 输入流
    private OutputStream mmOutStream;             // 输出流
    private ConnectThread mmConnectThread = null;         // 连接线程

    public static int Network_Send_flag = 0;      // 发送网络指令
    public ConnectedThread ct;                   // 通道管理线程
    private NetworkThread Networkmreadd = null;    // 定时器线程
    private int chuanshucishu = 0;                // 传输次数
    // public int lianjiecishu = 0;                // 连接次数
    public int mNetwork_Receive_flag = 0;                   // 网络接受标示


    public static double Kvdatacs = 0;                      // 系数
    public static double Kidatacs = 0;                      // 系数


    public static byte[] System_parameter = new byte[42];           //系统参数
    public static byte[] gbuffer = new byte[42];                   //接收缓冲
    private static byte[] com_send_buf = new byte[42];              //发送缓冲



    //发送数据到控制器实例话
    Controller_Data Controller_data = new Controller_Data();
    public static String Fan_unloading_current;
    private TextView mTvCommunicateState;
    private TextView mTvFanVoltage;
    private TextView mTvfanCurrent;
    private TextView mTvFanPower;
    private TextView mTvTotalPower;
    private TextView mTvFanState;
    private TextView mTvInverterVoltage;
    private TextView mTvInverterCurrent;
    private TextView mTvInverterPower;
    private TextView mTvWindSpeed;
    //private TextView mTvDeviceTemperature;
    private TextView mTvEquipmentFailureState;
    private CircleRangeView mCrvV;
    private CircleRangeView mCrvA;

    private String[] valueArray;
    private TextView mLoad1_A;
    private TextView mLoad2_A;

    /*
     *
     * 创建一个静态的Handler内部类，然后对Handler持有的对象使用弱应用，这样回收时也可以回收Handler持有对象
     * 这样虽然避免了泄露，不过在Looper线程的消息队列中还是有可能存在待处理的消息，所以在OnDestroy和OnStop时
     * 应该移除消息队列中的消息。
     * */
    //private MyHandler mMyHandler = new MyHandler(getContext());
    private TextView mTvDevicesName;
    private LinearLayout mLlSun;
    private LinearLayout mLlSunPower;
    private TextView mTvSunVoltage;
    private TextView mTvSunCurrent;
    private TextView mTvSunPower;
    private TextView mTvBatteryVoltage;
    private TextView mTvBatteryState;
    private TextView mTvBatteryTemperature;
    private TextView mTvOutSideWindSpeed;
    private LinearLayout mLlInverter;
    private LinearLayout mLlInverterPower;
    private View mView1;
    private View mView2;
    private View mView3;
    private View mView4;
    private View mView5;
    private View mView6;
    private View mView7;
    private View mView8;
    private View mView9;
    private View mView10;
    private View mView11;
    private LinearLayout mLlWindSpeed;
    private LinearLayout mLlOutSideWindSpeed;
    private LinearLayout mLlLoad1Load2;
    private View mView12;
    private LinearLayout mLlBattery;
    //private LinearLayout ;
    private LinearLayout mLlBatteryTemperature;
    byte type, addr;
    private NetworkThread mNetworkThread;
    /*    ------------------------------ 蓝牙部分 ---------------------------------       */
    private String mID3;
    private String mID1;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothDevice mBluetoothDevice;
    private BluetoothReadThread mBluetoothReadThread;

    public int timelj_flag0 = 0;
    public int lianjiecishu = 0;
    private BluetoothConnectThread mBluetoothConnectThread;
    private BluetoothSocket mBluetoothSocket;           // 连接套接字
    private BluetoothConnectedThread mBluetoothConnectedThread;
    // public static int Buletooth_Send_flag = 0;
    InputStream mInStream;             // 输入流
    private CircleRangeView mCrvV2;
    private CircleRangeView mCrvA2;
    private CircleRangeView mCrvV3;
    private CircleRangeView mCrvA3;
    private TextView mTvFanPower2;
    private ImageView mIvBatteryState;
    private ImageView mIvBackgroud;
    private TextView mTvFanState2;
    private TextView mTvSunPower2;
    private TextView mTvSunPower3;
    private LinearLayout mLlSunPower3;

    public void Network_Send(byte TYPE, byte ADDR) {
        // TYPE：类型，1字节
        // ADDR：地址，1字节，暂时不用

        type = TYPE;
        addr = ADDR;
        int i;
        if (type == 0 || type == 1 || type == 3)                  //--------读数据
        {
            com_send_buf[0] = (byte) 0xeb;
            com_send_buf[1] = (byte) 0x90;
            com_send_buf[2] = (byte) 0xeb;
            com_send_buf[3] = (byte) 0x90;
            com_send_buf[4] = addr;
            com_send_buf[5] = type;
            com_send_buf[6] = (byte) 0x00;
            com_send_buf[7] = (byte) 0x20;

            for (i = 8; i < 40; i++) {
                com_send_buf[i] = 0;
            }
        }
        if (type == 4)                                            //--------写参数
        {
            for (i = 0; i < 42; i++)
                com_send_buf[i] = System_parameter[i];                //保存控制器系统参数
            com_send_buf[0] = (byte) 0xeb;
            com_send_buf[1] = (byte) 0x90;
            com_send_buf[2] = (byte) 0xeb;
            com_send_buf[3] = (byte) 0x90;
            com_send_buf[4] = addr;
            com_send_buf[5] = type;
            com_send_buf[6] = (byte) 0x00;
            com_send_buf[7] = (byte) 0x20;

        }

        int sum = 0;
        for (i = 4; i < 40; i++) {
            sum += (com_send_buf[i] & 0x000000ff);
        }
        com_send_buf[40] = (byte) ((sum & 0x0000ff00) >> 8);
        com_send_buf[41] = (byte) (sum & 0x000000ff);

        if (mmInStream != null) {                //发送数据
            // 向网络发送请求指令
            write123(com_send_buf);

        } /*else {
            mTvCommunicateState.setText(getResources().getString(R.string.ConnectWithControllerFirstFlush));

        }*/
        // 蓝牙设备
        if (mInStream != null) {
            mBluetoothConnectedThread.write(com_send_buf);
        }
    }


    public void write123(byte[] bytes) {
        try {
            mmOutStream = mmSocket.getOutputStream();
            mmOutStream.write(bytes, 0, 42);
            mmOutStream.flush();
        } catch (IOException e) {
            //   Log.e("IOException",e.toString() );
        }
    }


    public ActualTimeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_actual_time, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mTvSunPower3 = (TextView) view.findViewById(R.id.tv_sun_power3);
        mLlSunPower3 = (LinearLayout) view.findViewById(R.id.ll_sun_power3);
        mIvBackgroud = (ImageView) view.findViewById(R.id.iv_background);
        mView1 = view.findViewById(R.id.view_1);
        mView2 = view.findViewById(R.id.view_2);
        mView3 = view.findViewById(R.id.view_3);
        mView4 = view.findViewById(R.id.view_4);
        mView5 = view.findViewById(R.id.view_5);
        mView6 = view.findViewById(R.id.view_6);
        mView7 = view.findViewById(R.id.view_7);
        mView8 = view.findViewById(R.id.view_8);
        mView9 = view.findViewById(R.id.view_9);
        mView10 = view.findViewById(R.id.view_10);
        mView11 = view.findViewById(R.id.view_11);
        mView12 = view.findViewById(R.id.view_12);
        //Load1和Load2
        mLlLoad1Load2 = (LinearLayout) view.findViewById(R.id.ll_load1_load2);
        //外界风速
        mLlOutSideWindSpeed = (LinearLayout) view.findViewById(R.id.ll_outside_wind_speed);
        //风机转速
        mLlWindSpeed = (LinearLayout) view.findViewById(R.id.ll_wind_speed);
        //逆变布局
        mLlInverter = (LinearLayout) view.findViewById(R.id.ll_Inverter);
        //逆变功率布局
        mLlInverterPower = (LinearLayout) view.findViewById(R.id.ll_Inverter_power);
        //光伏布局
        mLlSun = (LinearLayout) view.findViewById(R.id.ll_sun);
        //光伏功率
        mLlSunPower = (LinearLayout) view.findViewById(R.id.ll_sun_power);
        //光伏电压
        mTvSunVoltage = (TextView) view.findViewById(R.id.tv_sun_voltage);
        //光伏电流
        mTvSunCurrent = (TextView) view.findViewById(R.id.tv_sun_current);
        //光伏功率
        mTvSunPower = (TextView) view.findViewById(R.id.tv_sun_power);
        //电池部分布局
        mLlBattery = (LinearLayout) view.findViewById(R.id.ll_battery);

        //电池电压
        mTvBatteryVoltage = (TextView) view.findViewById(R.id.tv_battery_voltage);
        //电池状态
        mTvBatteryState = (TextView) view.findViewById(R.id.tv_battery_state);
        //电池温度
        mTvBatteryTemperature = (TextView) view.findViewById(R.id.tv_battery_temperature);
        //电池问题
        mLlBatteryTemperature = (LinearLayout) view.findViewById(R.id.ll_battery_temperature);
        //外界风速
        mTvOutSideWindSpeed = (TextView) view.findViewById(R.id.tv_outside_wind_speed);
        //设备名
        mTvDevicesName = (TextView) view.findViewById(R.id.tv_devices_name);
        //负载一电流
        mLoad1_A = (TextView) view.findViewById(R.id.load1_A);
        //负载二电流
        mLoad2_A = (TextView) view.findViewById(R.id.load2_A);

        //通信状态
        mTvCommunicateState = (TextView) view.findViewById(R.id.tv_communication_state);
        //风机电压
        mTvFanVoltage = (TextView) view.findViewById(R.id.tv_fan_voltage);
        //风机电流
        mTvfanCurrent = (TextView) view.findViewById(R.id.tv_fan_current);
        //风机功率
        mTvFanPower = (TextView) view.findViewById(R.id.tv_fan_power);
        mTvFanPower2 = (TextView) view.findViewById(R.id.tv_fan_power2);

        //累计发电
        mTvTotalPower = (TextView) view.findViewById(R.id.tv_total_power);
        // 风机状态
        mTvFanState = (TextView) view.findViewById(R.id.tv_fan_state);

        //逆变电压
        mTvInverterVoltage = (TextView) view.findViewById(R.id.tv_Inverter_voltage);

        //逆变电流
        mTvInverterCurrent = (TextView) view.findViewById(R.id.tv_Inverter_current);
        //逆变功率
        mTvInverterPower = (TextView) view.findViewById(R.id.tv_Inverter_power);
        //风机速度
        mTvWindSpeed = (TextView) view.findViewById(R.id.tv_Wind_Speed);
        //设备告警提示
        mTvEquipmentFailureState = (TextView) view.findViewById(R.id.tv_Equipment_Failure_State);
        //圆环
        mCrvV = (CircleRangeView) view.findViewById(R.id.crv_V);
        mCrvA = (CircleRangeView) view.findViewById(R.id.crv_A);
        valueArray = getResources().getStringArray(R.array.circlerangeview_values);
        //电池状态
        mIvBatteryState = (ImageView) view.findViewById(R.id.iv_battery_state);
        //风机状态
        mTvFanState2 = (TextView) view.findViewById(R.id.tv_fan_state2);

    }

    @Override
    public void onStart() {
        super.onStart();
        //注册EventBus
        EventBus.getDefault().register(this);

    }

    //接受广播
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {

        mID = event.getMessage();
        Log.e("mID", mID);
        if (mID.startsWith("GPRS")) {
            // 将code中的GPRS 去掉m
            mID1 = mID.replace("GPRS", "");
            // 通过Socket通信连接
            // 先判断有没有蓝牙通信 ，若有的话，先关闭
         /*   if (mBluetoothReadThread != null) {
                mBluetoothReadThread.interrupt();
                mBluetoothReadThread = null;
            }
            if (mBluetoothConnectThread != null) {
                mBluetoothConnectThread.interrupt();
                mBluetoothConnectThread = null;
            }
            if (mBluetoothConnectedThread != null) {
                mBluetoothConnectedThread.cancel();
                mBluetoothConnectedThread = null;
            }*/

        /*    try {
                mmSocket.close();
                mmInStream.close();
                mmOutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/

            ConncetDevices();

        } else if (mID.startsWith("RJ45")) {  //判断是不是以RJ45开头的的设备
            // 将code中的RJ45去掉
            mID1 = mID.replace("RJ45", "");
            ConncetDevices();

        } else {
            // 通过蓝牙通信连接设备
            // 先判断有没有Socket通信，有的话先关闭
          /*  try {
                mmSocket.close();
                mmInStream.close();
                mmOutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/

         /*  if (mmConnectThread != null){
                mmConnectThread.interrupt();
                mmConnectThread = null;
            }

            if (ct != null) {
                ct.interrupt();
                ct = null;
            }
            if (mNetworkThread != null) {
                mNetworkThread.interrupt();
                mNetworkThread = null;
            }*/

            // 将字符串转化为mac地址形式的
            String[] array = mID.split("");
            mID3 = array[1] + array[2] + ":" + array[3] + array[4] + ":" + array[5] + array[6] + ":" +
                    array[7] + array[8] + ":" + array[9] + array[10] + ":" + array[11] + array[12];

            //获取蓝牙适配器
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            BluetoothAdapter.getDefaultAdapter();
            //判断手机是否支持蓝牙
            if (mBluetoothAdapter == null) {
                // Device does not support Bluetooth
                Toast.makeText(getActivity(), getResources().getString(R.string.NoBluetoothFunction), Toast.LENGTH_SHORT).show();
            }
            //隐式打开蓝牙
            if (!mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.enable();
            }

            mBluetoothDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mID3);
            //开启发送数据请求指令线程
            mBluetoothReadThread = new BluetoothReadThread();
            mBluetoothReadThread.start();
            //连接蓝牙设备
            ConnectBluetoothDev();


        }
    }

    private void ConnectBluetoothDev() {
        if (mBluetoothDevice != null) {
            if (mBluetoothConnectedThread != null) {

            } else {
                // 开启蓝牙连接设备线程
                mBluetoothConnectThread = new BluetoothConnectThread(mBluetoothDevice);
                mBluetoothConnectThread.start();

                lianjiecishu = 1;
                timelj_flag0 = 5;

            }
        }

    }

    //  -------建立一个蓝牙套接字---输入参数是具体蓝牙对象---------
    // 要发起与远程设备（保持开放的服务器套接字的设备）的连接，
    // 必须首先获取表示该远程设备的 BluetoothDevice 对象。
    // 然后必须使用 BluetoothDevice 来获取 BluetoothSocket 并发起连接。
    public class BluetoothConnectThread extends Thread {

        // private final BluetoothDevice mmDevice;
        //  private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public BluetoothConnectThread(BluetoothDevice device) {
            BluetoothSocket tmp = null;
            mmDevice = device;
            try {
                UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
                tmp = mmDevice.createRfcommSocketToServiceRecord(uuid);

            } catch (IOException e) {
            }


            mBluetoothSocket = tmp;
        }

        public void run() {
            if (mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }

            try {
                mBluetoothSocket.connect();
            } catch (IOException connectException) {
                try {
                    mBluetoothSocket.close();
                } catch (IOException closeException) {

                }
                return;
            }
            mBluetoothConnectedThread = new BluetoothConnectedThread(mBluetoothSocket);
            mBluetoothConnectedThread.start();
        }
    }

    public class BluetoothConnectedThread extends Thread { // ------多线程类-------用于管理一个蓝牙套接字
        private final BluetoothSocket mBluetoothSocket;
        private final OutputStream mOutStream;

        public BluetoothConnectedThread(BluetoothSocket socket) {
            mBluetoothSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
            }
            mInStream = tmpIn;
            mOutStream = tmpOut;
            // 发送信息
            Message mTimeMessage = new Message();
            mTimeMessage.what = 1;
            mReceiveHandler.sendMessage(mTimeMessage);

        }

        @Override
        public void run() {
            //缓冲存储的流
            byte[] buffer = new byte[128];
            //从read()返回字节
            int bytes;
            int i_network = 0;
            // 继续监听InputStream直到发生异常
            while (true) {

                try {
                    // 从InputStream读取
                    bytes = mInStream.read(buffer);

                    //数据筛选用于排除无用 数据
                    for (i_network = 0; i_network < bytes; i_network++) {
                        if (mNetwork_Receive_flag == 0) {

                            if (buffer[i_network] == (byte) 0xeb) {
                                gbuffer[0] = (byte) 0xeb;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else if (mNetwork_Receive_flag == 1) {
                            if (buffer[i_network] == (byte) 0x90) {
                                gbuffer[1] = (byte) 0x90;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else if (mNetwork_Receive_flag == 2) {
                            if (buffer[i_network] == (byte) 0xeb) {
                                gbuffer[2] = (byte) 0xeb;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else if (mNetwork_Receive_flag == 3) {
                            if (buffer[i_network] == (byte) 0x90) {
                                gbuffer[3] = (byte) 0x90;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else {
                            if (mNetwork_Receive_flag < 42)               //接收
                            {
                                gbuffer[mNetwork_Receive_flag] = buffer[i_network];
                                mNetwork_Receive_flag++;
                            }
                            if (mNetwork_Receive_flag > 41 && mNetwork_Receive_flag != 50) {
                                int sum_i = 0;
                                int sumh = 0;
                                for (sum_i = 4; sum_i < 40; sum_i++) {
                                    sumh += (gbuffer[sum_i] & 0x000000ff);
                                }
                                if ((gbuffer[40] == (byte) ((sumh & 0x0000ff00) >> 8)) && (gbuffer[41] == (byte) (sumh & 0x000000ff))) {
                                    //已通过校验为合法数据帧
                                    //表示接收完成
                                    mNetwork_Receive_flag = 50;
                                } else
                                    mNetwork_Receive_flag = 0;
                            }

                        }
                    }
                    // 将获得的字节发送到UI并展示数据
                    mReceiveHandler.obtainMessage(801, bytes, -1, gbuffer)
                            .sendToTarget();

                } catch (IOException e) {
                    break;
                }

            }
        }

        /* Call this from the main Activity to send data to the remote device */


        public void write(byte[] bytes) {
            try {
                mOutStream.write(bytes);

            } catch (IOException e) {
            }
        }

        /* Call this from the main Activity to shutdown the connection */
        //关闭通信
        public void cancel() {
            try {
                mBluetoothSocket.close();
            } catch (IOException e) {
            }
        }
    }

    public class BluetoothReadThread extends Thread { // 此线程用于实现系统1S一次的定时作用

        public void run() {

            while (true) {

                try {

                    Thread.sleep(1000);
                    // mBluetoothConnectThread.start();
                    if (Network_Send_flag == 1)                              //当不知道控制器参数时发送读取参数
                        Network_Send((byte) 0x01, (byte) 0x00);
                    else if (Network_Send_flag == 2)                             //读取控制器数据
                        Network_Send((byte) 0x00, (byte) 0x00);
                    else if (Network_Send_flag == 3)                             //恢复参数
                        Network_Send((byte) 0x03, (byte) 0x00);
                    else if (Network_Send_flag == 4)                             //向控制器中写参数
                        Network_Send((byte) 0x04, (byte) 0x00);
                    // 发送数据请求指令给服务器
                    Message mTimeMessage = new Message();
                    mTimeMessage.what = 1;
                    mSendHandler.sendMessage(mTimeMessage);
                  //  Log.e("蓝牙测试", "蓝牙发送数据请求指令");

                } catch (InterruptedException e) {
                    break;
                }

            }
        }
    }

    public Handler mSendHandler = new Handler() { // ------------作为系统定时器使用

        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 1) {
                Log.e(TAG, "测试专用1: " + "发送数据请求");
                try {
                    //TextView textView = (TextView) findViewById(R.id.Monitor_TextView);
                    if (timelj_flag0 != 0)
                        timelj_flag0--;

                    if (lianjiecishu != 0 && timelj_flag0 == 0) {
                        lianjiecishu++;

                        mBluetoothConnectThread.start();
                        timelj_flag0 = 3; // 五秒钟连接一次
                    }

                /*    if (Network_Send_flag == 1)                              //当不知道控制器参数时发送读取参数
                        Network_Send((byte) 0x01, (byte) 0x00);
                    else if (Network_Send_flag == 2)                             //读取控制器数据
                        Network_Send((byte) 0x00, (byte) 0x00);
                    else if (Network_Send_flag == 3)                             //恢复参数
                        Network_Send((byte) 0x03, (byte) 0x00);
                    else if (Network_Send_flag == 4)                             //向控制器中写参数
                        Network_Send((byte) 0x04, (byte) 0x00);*/
                } catch (Exception e) {

                }
            }

        }
    };


    private void ConncetDevices() {
        //当程序一开始运行的时候就实例化Socket对象，与服务端进行连接获取输入输出流
        //因为4.0以后不能在主线程中进行网络操作，所以需要另外开辟一个线程
        //在子线程中连接Socket进行通信
        new Thread() {
            @Override
            public void run() {
                // 判断传过来的mID 是否与原来的old_ID相同，不做处理
                if (mID1.equals(old_ID)) {
                    Log.e("Tag", "设备相同");

                } else {
                    //  不同的话，重新赋值给old_ID
                    old_ID = mID1;
                    try {
                        //  根据mac地址和port端口号链接
                        if (mmSocket == null) {
                            mmSocket = new Socket("115.28.78.177", 65100);
                        } else {
                            // 当Socket 不为空时， 关闭Socket通信
                            mmSocket.close();
                            //  关闭连接线程，
                            if (mmConnectThread != null) {
                                mmConnectThread.interrupt();
                                mmConnectThread = null;
                            }
                            //  关闭发送请求指令线程
                            if (mNetworkThread != null) {
                                mNetworkThread.interrupt();
                                mNetworkThread = null;
                            }
                            // 重新创建Socket套接字
                            mmSocket = new Socket("115.28.78.177", 65100);
                        }

                        if (mmSocket.isConnected()) {      // 判断Socket对象是否连接成功
                            //获取保存数据密码
                            DeviceBean mDevicBean = SpUtils.getObject(MyApp.context, mID1);
                            String StrPassword = mDevicBean.getPassword().get(mDevicBean.getPassword().size()-1);

                            String strData = mID1 + StrPassword;    //账号密码
                            //       Log.e("StrData", strData);
                            //获的输出输出流
                            mmOutStream = mmSocket.getOutputStream();
                            mmInStream = mmSocket.getInputStream();
                            //向网络发送设备账号和密码
                            mmOutStream.write((strData + "\n").getBytes());   // 特别注意：数据的结尾加上换行符才可让服务器端的readline()停止阻塞
                            mmOutStream.flush();
                            //获取输入流
                            //创建byte对象
                            byte[] tmpbuf = new byte[1024];
                            //读取数据
                            int len = mmInStream.read(tmpbuf);

                            if (len == 18) {
                                // 创建连接线程
                                mmConnectThread = new ConnectThread(mmSocket);
                                mmConnectThread.start();
                                // 创建发送网络指令请求线程
                                mNetworkThread = new NetworkThread();
                                mNetworkThread.start();

                            } else {

                                if (mmSocket != null || mmInStream != null || mmOutStream != null) {
                                    try {
                                        //关闭输入输出流以及socket；
                                        mmInStream.close();
                                        mmOutStream.close();
                                        mmSocket.close();

                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                Network_Send_flag = 0;
                                mTvCommunicateState.setText(getResources().getString(R.string.AccountPassInCorrect)+","+getResources().getString(R.string.str_networkClickChangeDevice));
                            }

                        } else {
                            mmSocket.close();
                        }

                    } catch (UnknownHostException e) {
                        try {
                            mmSocket.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        e.printStackTrace();
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }

    // 用来存储数据
    public Handler mReceiveHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            try {
                switch (msg.what) {
                    case 801:
                        byte[] readBuf = (byte[]) msg.obj;
                        String tmp = "";
                        for (int i = 0; i < readBuf.length; i++) {
                            tmp = tmp + " " + readBuf[i];
                        }
                        Controller_data.Buf_Produce();
                        //先判断是不是特殊机型

                        if (mNetwork_Receive_flag == 50) {
                            if ((System_parameter[29] == 0x00)) {
                            //    Log.e("机型判断：", "普通机型");
                                mTvEquipmentFailureState.setVisibility(View.GONE); //不显示设备状态
                                if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) == 0) {
                                    //    Log.e("机型判断：","风光互补控制器");
                                    //风机电压仪表数据
                                    List<String> extras = new ArrayList<>();
                                    java.math.BigDecimal bigDey = new java.math.BigDecimal(Controller_data.Wind_V);
                                    float WindV = bigDey.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                                    DecimalFormat dfsf = new DecimalFormat("0.0");
                                    extras.add("风机电压:" + dfsf.format(WindV) + "V");




                                    if (((int) Controller_data.Wind_V) >= 0 && ((int) Controller_data.Wind_V) < 60) {
                                        mCrvV.setValueWithAnim(valueArray[0], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 60 && ((int) Controller_data.Wind_V) < 120) {
                                        mCrvV.setValueWithAnim(valueArray[1], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 120 && ((int) Controller_data.Wind_V) < 180) {
                                        mCrvV.setValueWithAnim(valueArray[2], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 240 && ((int) Controller_data.Wind_V) < 300) {
                                        mCrvV.setValueWithAnim(valueArray[3], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 300 && ((int) Controller_data.Wind_V) < 360) {
                                        mCrvV.setValueWithAnim(valueArray[4], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 360 && ((int) Controller_data.Wind_V) < 420) {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    } else {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    }

                                    //风机电流仪表的数据
                                    List<String> extras2 = new ArrayList<>();
                                    extras2.add("风机电流:" + Float.toString(Controller_data.Wind_A) + "A");
                                    if ("0.0".equals(Float.toString(Controller_data.Wind_A))) {
                                        mCrvA.setValueWithAnim(valueArray[0], extras2);

                                    } else {
                                        mCrvA.setValueWithAnim(valueArray[5], extras2);
                                    }
                                    //风机功率
                                    java.math.BigDecimal bigDec3 = new java.math.BigDecimal(Controller_data.Wind_W);
                                    // 数据四舍五入
                                    float WindPower = bigDec3.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                                    // 保留一位小数
                                    DecimalFormat df2 = new DecimalFormat("0.0");
                                    mTvFanPower.setText(df2.format(WindPower) + "W");

                                    mTvDevicesName.setText("风光互补控制器");
                                    // 隐藏逆变参数
                                    mLlInverter.setVisibility(View.GONE);
                                    mLlInverterPower.setVisibility(View.GONE);
                                    mView6.setVisibility(View.GONE);
                                    // 显示光伏数据
                                    mLlSun.setVisibility(View.VISIBLE);
                                    mView4.setVisibility(View.VISIBLE);
                                    mLlSunPower.setVisibility(View.VISIBLE);
                                    mView5.setVisibility(View.VISIBLE);
                                    //  显示电池数据
                                    mLlBattery.setVisibility(View.VISIBLE);
                                    mView8.setVisibility(View.VISIBLE);

                                    //  显示风机转速和外界风速
                                    mLlOutSideWindSpeed.setVisibility(View.VISIBLE);
                                    //  显示Load1和Load2
                                    mLlLoad1Load2.setVisibility(View.VISIBLE);
                                    // Load1 电流显示数据
                                       /*系统参数7位是硬件有无直流输出控制位 (判断有无负载)  高电平表示有直流输出，
                                         低表示无。（高电平负载是否显示）*/
                                    if ((System_parameter[8] & 0x80) != 0) {
                                        mLlLoad1Load2.setVisibility(View.VISIBLE);
                                        mView12.setVisibility(View.VISIBLE);

                                    } else {
                                        mLlLoad1Load2.setVisibility(View.GONE);
                                        mView12.setVisibility(View.GONE);

                                    }

                                    if ((gbuffer[9] & 0x10) != 0 && (int) (Controller_data.Load1_A) >= 0.5) {
                                        // 显示负载1和2的电流
                                        mLoad1_A.setText(Float.toString(Controller_data.Load1_A) + "A");

                                    } else if ((gbuffer[9] & 0x10) != 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        // 隐藏负载1和2的电流\
                                        mLoad1_A.setText(Float.toString(Controller_data.Load1_A) + "A");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));

                                    } else if ((gbuffer[9] & 0x10) == 0 && (int) (Controller_data.Load1_A) < 0.5) {
                                        mLoad1_A.setText("输出关断");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));

                                        // 根据数据DATA1： 0位表示过压，1位表示蓄电池欠压，
                                        if ((gbuffer[8] & 0x01) != 0) { // 蓄电池过压
                                            mTvBatteryState.setText("电池过压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad1_A.setText("过压");
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                            Picasso.with(getActivity()).load(R.mipmap.battery).into(mIvBatteryState);
                                        } else if ((gbuffer[8] & 0x02) != 0) {// 1位表示蓄电池欠压
                                            mTvBatteryState.setText("电池欠压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad1_A.setText("欠压");
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                            Picasso.with(getActivity()).load(R.mipmap.battery).into(mIvBatteryState);
                                        } else {
                                            mTvBatteryState.setText("电池正常");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.themeColor));
                                            Picasso.with(getActivity()).load(R.mipmap.battery_com).into(mIvBatteryState);
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        }

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) > 0.5) {
                                        mLoad1_A.setText("异常");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                    }
                                    // Load2的电流显示数据
                                    if ((gbuffer[9] & 0x02) != 0 && (int) (Controller_data.Load2_A) >= 0.5) {
                                        // 显示负载1和2的电流
                                        //  mLoad1_A.setText(Float.toString(Controller_data.Load2_A) + "A");
                                        mLoad2_A.setText(Float.toString(Controller_data.Load2_A) + "A");

                                    } else if ((gbuffer[9] & 0x20) != 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        // 隐藏负载1和2的电流\
                                        //   mLoad1_A.setText("无负载");
                                        mLoad2_A.setText(Float.toString(Controller_data.Load2_A) + "A");

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        mLoad2_A.setText("输出关断");
                                        mLoad2_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        if ((gbuffer[8] & 0x01) != 0) { // 蓄电池过压
                                            mTvBatteryState.setText("电池过压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad2_A.setText("过压");
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                        } else if ((gbuffer[8] & 0x02) != 0) {// 1位表示蓄电池欠压
                                            mTvBatteryState.setText("电池欠压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad2_A.setText("欠压");
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                        } else {
                                            mTvBatteryState.setText("电池正常");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.themeColor));
                                            //更换电池背景状态
                                            Picasso.with(getActivity()).load(R.mipmap.battery_com).into(mIvBatteryState);
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        }

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) > 0.5) {
                                        mLoad2_A.setText("异常");
                                        mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                    }

                                    //根据系统参数DATA2 的第五位是温度补偿控制位，高电平表示有温度补偿，那么才显示温度，不然就不了
                                    if ((System_parameter[9] & 0x20) != 0) {
                                        // 显示温度
                                        mLlBatteryTemperature.setVisibility(View.VISIBLE);
                                        mView9.setVisibility(View.VISIBLE);

                                    } else {
                                        // 隐藏温度
                                        mLlBatteryTemperature.setVisibility(View.GONE);
                                        mView9.setVisibility(View.GONE);
                                    }

                                    //根据系统参数DATA2 :0位是转速控制位，高电平表示有转速控制。无转速控制时不显示转速和相应设定（风机转速）
                                    if ((System_parameter[9] & 0x01) != 0) {
                                        // 显示风机转速
                                        mLlWindSpeed.setVisibility(View.VISIBLE);
                                        mView10.setVisibility(View.VISIBLE);

                                    } else {
                                        // 隐藏风机转速
                                        mLlWindSpeed.setVisibility(View.GONE);
                                        mView10.setVisibility(View.GONE);
                                    }
                                    // 根据系统参数DATA2:7位是风速控制位（外界风速）
                                    if ((System_parameter[9] & 0x80) != 0) {
                                        // 显示外界风速
                                        mLlOutSideWindSpeed.setVisibility(View.VISIBLE);
                                        mView10.setVisibility(View.VISIBLE);

                                    } else {
                                        // 隐藏外界风速
                                        mLlOutSideWindSpeed.setVisibility(View.GONE);
                                        mView10.setVisibility(View.GONE);

                                    }
                                } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) == 0) {  //  纯光伏控制器
                                    //显示内容：光伏电流 光伏电压 光伏功率 累计发电 电池电压 电池状态 电池温度 负载1 和 负载2
                                    mTvDevicesName.setText("光伏控制器");
                                    mTvEquipmentFailureState.setVisibility(View.GONE);

                                    List<String> extras = new ArrayList<>();
                                    // extras.add("风机电压(V)：48");
                                    extras.add("光伏电压:" + Float.toString(Controller_data.Sun_V) + "V");

                                    if (((int) Controller_data.Sun_V) >= 0 && ((int) Controller_data.Sun_V) < 60) {
                                        mCrvV.setValueWithAnim(valueArray[0], extras);
                                    } else if (((int) Controller_data.Sun_V) >= 60 && ((int) Controller_data.Sun_V) < 120) {
                                        mCrvV.setValueWithAnim(valueArray[1], extras);
                                    } else if (((int) Controller_data.Sun_V) >= 120 && ((int) Controller_data.Sun_V) < 180) {
                                        mCrvV.setValueWithAnim(valueArray[2], extras);
                                    } else if (((int) Controller_data.Sun_V) >= 240 && ((int) Controller_data.Sun_V) < 300) {
                                        mCrvV.setValueWithAnim(valueArray[3], extras);
                                    } else if (((int) Controller_data.Sun_V) >= 300 && ((int) Controller_data.Sun_V) < 360) {
                                        mCrvV.setValueWithAnim(valueArray[4], extras);
                                    } else if (((int) Controller_data.Sun_V) >= 360 && ((int) Controller_data.Sun_V) < 420) {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    } else {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    }

                                    //风机电流仪表的数据
                                    List<String> extras2 = new ArrayList<>();
                                    extras2.add("光伏电流:" + Float.toString(Controller_data.Sun_A) + "A");
                                    if ("0.0".equals(Float.toString(Controller_data.Sun_A))) {
                                        mCrvA.setValueWithAnim(valueArray[0], extras2);

                                    } else {
                                        mCrvA.setValueWithAnim(valueArray[5], extras2);
                                    }
                                    //隐藏光伏数据
                                    mLlSun.setVisibility(View.GONE);

                                    // 隐藏光伏功率
                                    mLlSunPower.setVisibility(View.GONE);
                                    mView5.setVisibility(View.GONE);
                                    mView4.setVisibility(View.GONE);
                                    mView6.setVisibility(View.GONE);
                                    mView2.setVisibility(View.VISIBLE);
                                    // 显示累计发电量

                                    // 显示电池电压 电池状态 电池温度
                                    mLlBattery.setVisibility(View.VISIBLE);
                                    mView8.setVisibility(View.VISIBLE);
                                    // 温度显示
                                    if ((System_parameter[9] & 0x20) != 0) {
                                        mLlBatteryTemperature.setVisibility(View.VISIBLE);
                                        mView9.setVisibility(View.VISIBLE);
                                    } else {
                                        mLlBatteryTemperature.setVisibility(View.GONE);
                                        mView9.setVisibility(View.GONE);

                                    }


                                      // 显示负载1电流 ， 负载2 电流
                                       /*系统参数7位是硬件有无直流输出控制位 (判断有无负载)  高电平表示有直流输出，
                                         低表示无。（高电平负载是否显示）*/
                                    if ((System_parameter[8] & 0x80) != 0) {
                                        //有直流输出，显示Load1和Load2
                                        mLlLoad1Load2.setVisibility(View.VISIBLE);
                                        mView12.setVisibility(View.VISIBLE);
                                    } else {
                                        mLlLoad1Load2.setVisibility(View.GONE);
                                        mView12.setVisibility(View.GONE);
                                    }

                                    if ((gbuffer[9] & 0x10) != 0 && (int) (Controller_data.Load1_A) >= 0.5) {
                                        // 显示负载1和2的电流
                                        mLoad1_A.setText(Float.toString(Controller_data.Load1_A) + "A");

                                    } else if ((gbuffer[9] & 0x10) != 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        // 隐藏负载1和2的电流\
                                        mLoad1_A.setText(Float.toString(Controller_data.Load1_A) + "A");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));

                                    } else if ((gbuffer[9] & 0x10) == 0 && (int) (Controller_data.Load1_A) < 0.5) {
                                        mLoad1_A.setText("输出关断");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));

                                        // 根据数据DATA1： 0位表示过压，1位表示蓄电池欠压，
                                        if ((gbuffer[8] & 0x01) != 0) { // 蓄电池过压
                                            mTvBatteryState.setText("电池过压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad1_A.setText("过压");
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                            Picasso.with(getActivity()).load(R.mipmap.battery).into(mIvBatteryState);
                                        } else if ((gbuffer[8] & 0x02) != 0) {// 1位表示蓄电池欠压
                                            mTvBatteryState.setText("电池欠压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad1_A.setText("欠压");
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                            Picasso.with(getActivity()).load(R.mipmap.battery).into(mIvBatteryState);
                                        } else {
                                            mTvBatteryState.setText("电池正常");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.themeColor));
                                            Picasso.with(getActivity()).load(R.mipmap.battery_com).into(mIvBatteryState);
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        }

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) > 0.5) {
                                        mLoad1_A.setText("异常");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                    }
                                    // Load2的电流显示数据
                                    if ((gbuffer[9] & 0x02) != 0 && (int) (Controller_data.Load2_A) >= 0.5) {
                                        // 显示负载1和2的电流
                                        mLoad2_A.setText(Float.toString(Controller_data.Load2_A) + "A");

                                    } else if ((gbuffer[9] & 0x20) != 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        // 隐藏负载1和2的电流\
                                        mLoad2_A.setText(Float.toString(Controller_data.Load2_A) + "A");

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        mLoad2_A.setText("输出关断");
                                        mLoad2_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        if ((gbuffer[8] & 0x01) != 0) { // 蓄电池过压
                                            mTvBatteryState.setText("电池过压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad2_A.setText("过压");
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                        } else if ((gbuffer[8] & 0x02) != 0) {// 1位表示蓄电池欠压
                                            mTvBatteryState.setText("电池欠压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad2_A.setText("欠压");
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                        } else {
                                            mTvBatteryState.setText("电池正常");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.themeColor));
                                            // 更换图片
                                            Picasso.with(getActivity()).load(R.mipmap.battery_com).into(mIvBatteryState);
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        }


                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) > 0.5) {
                                        mLoad2_A.setText("异常");
                                        mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                    }

                                    // 隐藏风机功率数据
                                    mTvFanPower.setVisibility(View.GONE);
                                    mTvFanPower2.setVisibility(View.GONE);
                                    // 隐藏风机状态
                                    mTvFanState2.setVisibility(View.GONE);
                                    mTvFanState.setVisibility(View.GONE);
                                    mLlSunPower3.setVisibility(View.VISIBLE);
                                    // 光伏功率
                                    java.math.BigDecimal bigDec = new java.math.BigDecimal(Controller_data.Sun_W);
                                    float SunPower = bigDec.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                                    DecimalFormat df = new DecimalFormat("0.0");
                                    mTvSunPower3.setText(df.format(SunPower) + "W");
                                    // 隐藏逆变数据
                                    mLlInverter.setVisibility(View.GONE);
                                    mLlInverterPower.setVisibility(View.GONE);

                                    //隐藏风机转速 外界速度
                                    mLlWindSpeed.setVisibility(View.GONE);
                                    mLlOutSideWindSpeed.setVisibility(View.GONE);

                                    //隐藏设备温度
                                    mView10.setVisibility(View.GONE);


                                } else if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) != 0) {
                                    mTvDevicesName.setText("风机控制器");

                                    //风机电压仪表数据
                                    List<String> extras = new ArrayList<>();
                                    java.math.BigDecimal bigDe = new java.math.BigDecimal(Controller_data.Wind_V);
                                    float WindV = bigDe.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                                    DecimalFormat dff = new DecimalFormat("0.0");
                                    extras.add("风机电压:" + dff.format(WindV) + "V");

                                    if (((int) Controller_data.Wind_V) >= 0 && ((int) Controller_data.Wind_V) < 60) {
                                        mCrvV.setValueWithAnim(valueArray[0], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 60 && ((int) Controller_data.Wind_V) < 120) {
                                        mCrvV.setValueWithAnim(valueArray[1], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 120 && ((int) Controller_data.Wind_V) < 180) {
                                        mCrvV.setValueWithAnim(valueArray[2], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 240 && ((int) Controller_data.Wind_V) < 300) {
                                        mCrvV.setValueWithAnim(valueArray[3], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 300 && ((int) Controller_data.Wind_V) < 360) {
                                        mCrvV.setValueWithAnim(valueArray[4], extras);
                                    } else if (((int) Controller_data.Wind_V) >= 360 && ((int) Controller_data.Wind_V) < 420) {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    } else {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    }

                                    //风机电流仪表的数据
                                    List<String> extras2 = new ArrayList<>();
                                    extras2.add("风机电流:" + Float.toString(Controller_data.Wind_A) + "A");
                                    if ("0.0".equals(Float.toString(Controller_data.Wind_A))) {
                                        mCrvA.setValueWithAnim(valueArray[0], extras2);

                                    } else {
                                        mCrvA.setValueWithAnim(valueArray[5], extras2);
                                    }
                                    //风机功率
                                    BigDecimal bigDec3 = new BigDecimal(Controller_data.Wind_W);
                                    float WindPower = bigDec3.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
                                    DecimalFormat df2 = new DecimalFormat("0.0");
                                    mTvFanPower.setText(df2.format(WindPower) + "W");

                                    // 隐藏逆变参数
                                    mLlInverter.setVisibility(View.GONE);
                                    mLlInverterPower.setVisibility(View.GONE);
                                    mView6.setVisibility(View.GONE);
                                    // 显示光伏参数
                                    mLlSun.setVisibility(View.GONE);
                                    mView4.setVisibility(View.GONE);
                                    mLlSunPower.setVisibility(View.GONE);
                                    mView5.setVisibility(View.GONE);
                                    //  显示电池参数
                                    mLlBattery.setVisibility(View.VISIBLE);
                                    mView8.setVisibility(View.VISIBLE);

                                    //  显示风机转速和外界风速
                                    mLlOutSideWindSpeed.setVisibility(View.VISIBLE);
                                    //  显示Load1和Load2
                                    mLlLoad1Load2.setVisibility(View.VISIBLE);
                                    //  Load1 电流显示数据
                                       /*系统参数7位是硬件有无直流输出控制位 (判断有无负载)  高电平表示有直流输出，
                                         低表示无。（高电平负载是否显示）*/
                                    if ((System_parameter[8] & 0x80) != 0) {
                                        mLlLoad1Load2.setVisibility(View.VISIBLE);
                                        mView12.setVisibility(View.VISIBLE);

                                    } else {
                                        mLlLoad1Load2.setVisibility(View.GONE);
                                        mView12.setVisibility(View.GONE);

                                    }

                                    if ((gbuffer[9] & 0x10) != 0 && (int) (Controller_data.Load1_A) >= 0.5) {
                                        // 显示负载1和2的电流
                                        mLoad1_A.setText(Float.toString(Controller_data.Load1_A) + "A");

                                    } else if ((gbuffer[9] & 0x10) != 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        // 隐藏负载1和2的电流\
                                        mLoad1_A.setText(Float.toString(Controller_data.Load1_A) + "A");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));

                                    } else if ((gbuffer[9] & 0x10) == 0 && (int) (Controller_data.Load1_A) < 0.5) {
                                        mLoad1_A.setText("输出关断");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));

                                        // 根据数据DATA1： 0位表示过压，1位表示蓄电池欠压，
                                        if ((gbuffer[8] & 0x01) != 0) { // 蓄电池过压
                                            mTvBatteryState.setText("电池过压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad1_A.setText("过压");
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                            Picasso.with(getActivity()).load(R.mipmap.battery).into(mIvBatteryState);
                                        } else if ((gbuffer[8] & 0x02) != 0) {// 1位表示蓄电池欠压
                                            mTvBatteryState.setText("电池欠压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad1_A.setText("欠压");
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                            Picasso.with(getActivity()).load(R.mipmap.battery).into(mIvBatteryState);
                                        } else {
                                            mTvBatteryState.setText("电池正常");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.themeColor));
                                            Picasso.with(getActivity()).load(R.mipmap.battery_com).into(mIvBatteryState);
                                            mLoad1_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        }

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) > 0.5) {
                                        mLoad1_A.setText("异常");
                                        mLoad1_A.setTextColor(getResources().getColor(R.color.colorRed));
                                    }
                                    // Load2的电流显示数据
                                    if ((gbuffer[9] & 0x02) != 0 && (int) (Controller_data.Load2_A) >= 0.5) {
                                        // 显示负载1和2的电流

                                        mLoad2_A.setText(Float.toString(Controller_data.Load2_A) + "A");

                                    } else if ((gbuffer[9] & 0x20) != 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        // 隐藏负载1和2的电流

                                        mLoad2_A.setText(Float.toString(Controller_data.Load2_A) + "A");

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) < 0.5) {

                                        mLoad2_A.setText("输出关断");
                                        mLoad2_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        if ((gbuffer[8] & 0x01) != 0) { // 蓄电池过压
                                            mTvBatteryState.setText("电池过压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad2_A.setText("过压");
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                        } else if ((gbuffer[8] & 0x02) != 0) {// 1位表示蓄电池欠压
                                            mTvBatteryState.setText("电池欠压");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.colorRed));
                                            mLoad2_A.setText("欠压");
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                        } else {
                                            mTvBatteryState.setText("电池正常");
                                            mTvBatteryState.setTextColor(getResources().getColor(R.color.themeColor));
                                            Picasso.with(getActivity()).load(R.mipmap.battery_com).into(mIvBatteryState);
                                            mLoad2_A.setTextColor(getResources().getColor(R.color.themeColor));
                                        }

                                    } else if ((gbuffer[9] & 0x20) == 0 && (int) (Controller_data.Load1_A) > 0.5) {
                                        mLoad2_A.setText("异常");
                                        mLoad2_A.setTextColor(getResources().getColor(R.color.colorRed));
                                    }

                                    //根据系统参数DATA2 的第五位是温度补偿控制位，高电平表示有温度补偿，那么才显示温度，不然就不了
                                    if ((System_parameter[9] & 0x20) != 0) {
                                        mLlBatteryTemperature.setVisibility(View.VISIBLE);
                                        mView9.setVisibility(View.VISIBLE);
                                    } else {
                                        // 隐藏温度
                                        mLlBatteryTemperature.setVisibility(View.GONE);
                                        mView9.setVisibility(View.GONE);
                                    }
                                    //根据系统参数DATA2 :0位是转速控制位，高电平表示有转速控制。无转速控制时不显示转速和相应设定（风机转速）
                                    if ((System_parameter[9] & 0x01) != 0) {
                                        // 显示风机转速
                                        mLlWindSpeed.setVisibility(View.VISIBLE);
                                        mView10.setVisibility(View.VISIBLE);

                                    } else {

                                        mLlWindSpeed.setVisibility(View.GONE);
                                        mView10.setVisibility(View.GONE);
                                        // 隐藏风机转速

                                    }
                                    //根据系统参数DATA2:7位是风速控制位（外界风速）
                                    if ((System_parameter[9] & 0x80) != 0) {
                                        // 显示外界风速
                                        mLlOutSideWindSpeed.setVisibility(View.VISIBLE);
                                        mView11.setVisibility(View.VISIBLE);

                                    } else {
                                        // 隐藏外界风速
                                        mLlOutSideWindSpeed.setVisibility(View.GONE);
                                        mView11.setVisibility(View.GONE);
                                    }

                                } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) != 0) {
                                    mTvDevicesName.setText("逆变器");
                                    /*
                                     * 显示逆变电流 和 逆变电压
                                     * */
                                    List<String> extras = new ArrayList<>();
                                    extras.add("逆变电压:" + Float.toString(Controller_data.nibian_V) + "V");

                                    if (((int) Controller_data.nibian_V) >= 0 && ((int) Controller_data.nibian_V) < 60) {
                                        mCrvV.setValueWithAnim(valueArray[0], extras);
                                    } else if (((int) Controller_data.nibian_V) >= 60 && ((int) Controller_data.nibian_V) < 120) {
                                        mCrvV.setValueWithAnim(valueArray[1], extras);
                                    } else if (((int) Controller_data.nibian_V) >= 120 && ((int) Controller_data.nibian_V) < 180) {
                                        mCrvV.setValueWithAnim(valueArray[2], extras);
                                    } else if (((int) Controller_data.nibian_V) >= 240 && ((int) Controller_data.nibian_V) < 300) {
                                        mCrvV.setValueWithAnim(valueArray[3], extras);
                                    } else if (((int) Controller_data.nibian_V) >= 300 && ((int) Controller_data.nibian_V) < 360) {
                                        mCrvV.setValueWithAnim(valueArray[4], extras);
                                    } else if (((int) Controller_data.nibian_V) >= 360 && ((int) Controller_data.nibian_V) < 420) {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    } else {
                                        mCrvV.setValueWithAnim(valueArray[5], extras);
                                    }

                                    //风机电流仪表的数据
                                    List<String> extras2 = new ArrayList<>();
                                    extras2.add("逆变电流:" + Float.toString(Controller_data.nibian_A) + "A");
                                    if ("0.0".equals(Float.toString(Controller_data.nibian_A))) {

                                        mCrvA.setValueWithAnim(valueArray[0], extras2);

                                    } else {
                                        mCrvA.setValueWithAnim(valueArray[5], extras2);
                                    }
                                    // 隐藏逆变数据
                                    mLlInverter.setVisibility(View.GONE);
                                    mView6.setVisibility(View.GONE);
                                    mLlInverterPower.setVisibility(View.VISIBLE);
                                    //隐藏风机数据
                                    llFan.setVisibility(View.GONE);
                                    mView2.setVisibility(View.GONE);
                                    //隐藏累计电量
                                    llTotalPower.setVisibility(View.GONE);
                                    mView3.setVisibility(View.GONE);
                                    //隐藏光伏数据
                                    mLlSun.setVisibility(View.GONE);
                                    mView4.setVisibility(View.GONE);
                                    //隐藏光伏功率数据
                                    mLlSunPower.setVisibility(View.GONE);
                                    mView5.setVisibility(View.GONE);
                                    //显示电池数据
                                    mLlBattery.setVisibility(View.VISIBLE);
                                    mView8.setVisibility(View.VISIBLE);
                                    //显示电池温度数据
                                    mLlBatteryTemperature.setVisibility(View.VISIBLE);
                                    mView9.setVisibility(View.VISIBLE);
                                    // 温度
/*
                                    if ((System_parameter[9] & 0x20) != 0) {
                                        mLlBatteryTemperature.setVisibility(View.VISIBLE);
                                        mView9.setVisibility(View.VISIBLE);
                                    } else {
                                        // 隐藏温度
                                        mLlBatteryTemperature.setVisibility(View.GONE);
                                        mView9.setVisibility(View.GONE);
                                    }*/


                                }


                            } else {
                                //  特殊机型 10千瓦逆变一体机
                                /* if ((gbuffer[29] & 0xf7) == 0xf7) { }*/
                                mTvDevicesName.setText("风机控制逆变一体机");
                                mTvEquipmentFailureState.setVisibility(View.VISIBLE); //不显示设备状态
                                // 显示逆变数据
                                mLlInverter.setVisibility(View.VISIBLE);
                                mView6.setVisibility(View.VISIBLE);
                                // 显示逆变功率
                                mLlInverterPower.setVisibility(View.VISIBLE);
                                mView7.setVisibility(View.VISIBLE);
                                // 更换图片
                                Picasso.with(getActivity()).load(R.mipmap.inverter).into(mIvBackgroud);
                                //风机电压仪表数据
                                List<String> extras = new ArrayList<>();

                                extras.add("风机电压:" + Float.toString(Controller_data.Wind_V) + "V");
                              //  Log.e(" 风机电压:", Float.toString(Controller_data.Wind_V));

                                if (((int) Controller_data.Wind_V) >= 0 && ((int) Controller_data.Wind_V) < 60) {
                                    mCrvV.setValueWithAnim(valueArray[0], extras);
                                } else if (((int) Controller_data.Wind_V) >= 60 && ((int) Controller_data.Wind_V) < 120) {
                                    mCrvV.setValueWithAnim(valueArray[1], extras);
                                } else if (((int) Controller_data.Wind_V) >= 120 && ((int) Controller_data.Wind_V) < 180) {
                                    mCrvV.setValueWithAnim(valueArray[2], extras);
                                } else if (((int) Controller_data.Wind_V) >= 240 && ((int) Controller_data.Wind_V) < 300) {
                                    mCrvV.setValueWithAnim(valueArray[3], extras);
                                } else if (((int) Controller_data.Wind_V) >= 300 && ((int) Controller_data.Wind_V) < 360) {
                                    mCrvV.setValueWithAnim(valueArray[4], extras);
                                } else if (((int) Controller_data.Wind_V) >= 360 && ((int) Controller_data.Wind_V) < 420) {
                                    mCrvV.setValueWithAnim(valueArray[5], extras);
                                } else {
                                    mCrvV.setValueWithAnim(valueArray[5], extras);
                                }

                                //风机电流仪表的数据
                                List<String> extras2 = new ArrayList<>();
                                extras2.add("风机电流:" + Float.toString(Controller_data.Wind_A) + "A");
                                if ("0.0".equals(Float.toString(Controller_data.Wind_A))) {
                                    mCrvA.setValueWithAnim(valueArray[0], extras2);

                                } else {
                                    mCrvA.setValueWithAnim(valueArray[5], extras2);
                                }
                                //  设置风机功率
                                java.math.BigDecimal bigDec3 = new java.math.BigDecimal(Controller_data.Wind_W);
                                float WindPower = bigDec3.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                                DecimalFormat df2 = new DecimalFormat("0.0");
                                mTvFanPower.setText(df2.format(WindPower) + "W");
                                //  隐藏光伏数据
                                mLlSun.setVisibility(View.GONE);
                                mView4.setVisibility(View.GONE);
                                //  隐藏光伏功率数据
                                mLlSunPower.setVisibility(View.GONE);
                                mView5.setVisibility(View.GONE);

                                // 温度
                                if ((System_parameter[9] & 0x20) != 0) {
                                    // 展示温度
                                    mLlBatteryTemperature.setVisibility(View.VISIBLE);
                                    mView9.setVisibility(View.VISIBLE);
                                } else {
                                    // 隐藏温度
                                    mLlBatteryTemperature.setVisibility(View.GONE);
                                    mView9.setVisibility(View.GONE);
                                }
                                //2 .根据系统参数DATA2 :0位是转速控制位，高电平表示有转速控制。无转速控制时不显示转速和相应设定（风机转速）
                                if ((System_parameter[9] & 0x01) != 0) {
                                    // 显示风机转速
                                    mLlWindSpeed.setVisibility(View.VISIBLE);
                                    mView10.setVisibility(View.VISIBLE);
                                } else {
                                    // 隐藏风机转速
                                    mLlWindSpeed.setVisibility(View.GONE);
                                    mView10.setVisibility(View.GONE);
                                }

                                //3.根据系统参数DATA2:7位是风速控制位（外界风速）
                                if ((System_parameter[9] & 0x80) != 0) {
                                    // 显示外界风速
                                    mLlOutSideWindSpeed.setVisibility(View.VISIBLE);
                                   mView11.setVisibility(View.VISIBLE);
                                   mView10.setVisibility(View.GONE);


                                } else {
                                    // 隐藏外界风速
                                    mLlOutSideWindSpeed.setVisibility(View.GONE);
                                    mView11.setVisibility(View.GONE);

                                }


                            }


                            chuanshucishu++;
                            // 电池状态
                            String battery_state = new String();
                            // 风机状态
                            String mWind_state = new String();
                            String load1_state = new String();
                            String load2_state = new String();

                            battery_state = getResources().getString(R.string.Normal);

                            mWind_state = getResources().getString(R.string.RunNormal);
                            load1_state = getResources().getString(R.string.Normal);
                            load2_state = getResources().getString(R.string.Normal);

                            //  风机
                            if (Controller_data.Unload_Load) {   //风机卸载
                                mWind_state = getResources().getString(R.string.Dumpload);
                                mTvFanState.setTextColor(getResources().getColor(R.color.colorRed));
                                mTvFanState.setText(mWind_state);

                                mTvWindSpeed.setText(mWind_state);
                                mTvWindSpeed.setTextColor(getResources().getColor(R.color.colorRed));


                            } else if (Controller_data.Overload_Speed) { // 过速刹车
                                mWind_state = getResources().getString(R.string.OverRotateSpeedBrake);
                                mTvFanState.setTextColor(getResources().getColor(R.color.colorRed));
                                mTvFanState.setText(mWind_state);
                                mTvWindSpeed.setText(Float.toString(Controller_data.Wind_Speed) + "Rpm");
                                mTvWindSpeed.setTextColor(getResources().getColor(R.color.themeColor));
                            } else if (Controller_data.Hand_Stop) {  // 手动刹车
                                mWind_state = getResources().getString(R.string.ManualBrake);
                                mTvFanState.setTextColor(getResources().getColor(R.color.colorRed));
                                mTvFanState.setText(mWind_state);
                                mTvWindSpeed.setText(Float.toString(Controller_data.Wind_Speed) + "Rpm");
                                mTvWindSpeed.setTextColor(getResources().getColor(R.color.themeColor));

                            } else {
                                //风机状态：正常
                                mTvFanState.setTextColor(getResources().getColor(R.color.themeColor));
                                mTvFanState.setText(mWind_state);
                                mTvWindSpeed.setText(Float.toString(Controller_data.Wind_Speed) + "Rpm");
                                mTvWindSpeed.setTextColor(getResources().getColor(R.color.themeColor));
                            }


                            if (Controller_data.Load1_On)
                                load1_state = "On";
                            else
                                load1_state = "Off";

                            if (Controller_data.Load2_On)
                                load2_state = "On";
                            else
                                load2_state = "Off";

                            if (Controller_data.Load_Overload) {
                                load1_state = getResources().getString(R.string.Overload);
                                load2_state = getResources().getString(R.string.Overload);
                                //  mTvEquipmentFailureState.setVisibility(View.VISIBLE);
                                //  mTvEquipmentFailureState.setText(getResources().getString(R.string.LoadOverload));
                            }

                            if (Controller_data.Load_Overload) {
                                load1_state = getResources().getString(R.string.ShortCircuit);
                                load2_state = getResources().getString(R.string.ShortCircuit);
                                //  mTvEquipmentFailureState.setVisibility(View.VISIBLE);
                                //  mTvEquipmentFailureState.setText(getResources().getString(R.string.LoadShortCircuit));
                            }
                            if (Controller_data.Battery_H) {
                                battery_state = getResources().getString(R.string.OverVoltage);
                                mTvBatteryState.setText(battery_state);
                                //   mTvEquipmentFailureState.setVisibility(View.VISIBLE);
                                //  mTvEquipmentFailureState.setText(getResources().getString(R.string.BattOverVoltage));
                            } else if (Controller_data.Battery_L) {
                                battery_state = getResources().getString(R.string.Undervolt);
                                mTvBatteryState.setText(battery_state);
                                //   mTvEquipmentFailureState.setVisibility(View.VISIBLE);
                                //  mTvEquipmentFailureState.setText(getResources().getString(R.string.BattUnderVoltage));

                            } else {
                                mTvBatteryState.setText("电池正常");
                            }
                            if (gbuffer[9] == 1) {
                                //告警提示：逆变侧a相过流 (字体红色)
                                mTvEquipmentFailureState.setText("告警提示：逆变侧a相过流");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));
                            } else if (gbuffer[9] == 2) {
                                //"告警提示：逆变侧b相过流" (字体红色)
                                mTvEquipmentFailureState.setText("告警提示：逆变侧b相过流");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));
                            } else if (gbuffer[9] == 3) {
                                //"告警提示：逆变侧c相过流" (字体红色)
                                mTvEquipmentFailureState.setText("告警提示：逆变侧c相过流");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));

                            } else if (gbuffer[9] == 4) {
                                //"告警提示：直流母线过压"
                                mTvEquipmentFailureState.setText("告警提示：直流母线过压");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));

                            } else if (Controller_data.Tempreture_Over) {
                                //"告警提示：过温保护"
                                mTvEquipmentFailureState.setText("告警提示：过温保护");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));
                            } else if (gbuffer[9] == 8) {
                                //"告警提示：逆变侧IPM故障保护"
                                mTvEquipmentFailureState.setText("告警提示：逆变侧IPM故障保护");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));

                            } else if (gbuffer[9] == 20) {
                                // "告警提示：风机侧a相过流"
                                mTvEquipmentFailureState.setText("告警提示：风机侧a相过流");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));
                            } else if (gbuffer[9] == 21) {
                                //告警提示：风机侧b相过流
                                mTvEquipmentFailureState.setText("告警提示：风机侧b相过流");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));

                            } else if (gbuffer[9] == 22) {
                                //"告警提示：风机侧c相过流"
                                mTvEquipmentFailureState.setText("告警提示：风机侧c相过流");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));

                            } else if (gbuffer[9] == 23) {
                                //告警提示：风机侧IPM故障保护"
                                mTvEquipmentFailureState.setText("告警提示：风机侧IPM故障保护");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorRed));

                            } else if (gbuffer[9] == 40) {
                                //告警提示：无市电  (字体蓝色)
                                mTvEquipmentFailureState.setText("告警提示：无市电");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorBlue));

                            } else if (gbuffer[9] == 41) {
                                //告警提示：无市电  (字体蓝色)
                                mTvEquipmentFailureState.setText("告警提示：无市电");
                                mTvEquipmentFailureState.setTextColor(getResources().getColor(R.color.colorBlue));
                            }

                            //    Log.e("起始电压点a",Float.toString((float) Math.round((System_parameter[22] & 0xff) * (float) Kvdatacs * 10) / 10));
                            //光伏电压
                            mTvSunVoltage.setText(Float.toString(Controller_data.Sun_V) + "V");
                            //光伏电流
                            mTvSunCurrent.setText(Float.toString(Controller_data.Sun_A) + "A");
                            //光伏功率

                            java.math.BigDecimal bigDec = new java.math.BigDecimal(Controller_data.Sun_W);
                            float SunPower = bigDec.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                            DecimalFormat df = new DecimalFormat("0.0");

                            mTvSunPower.setText(df.format(SunPower) + "W");

                            //电池电压
                            java.math.BigDecimal bigD = new java.math.BigDecimal(Controller_data.Battery_V);
                            float BatteryV = bigD.setScale(1, java.math.BigDecimal.ROUND_HALF_UP).floatValue();
                            DecimalFormat dfR = new DecimalFormat("0.0");

                            mTvBatteryVoltage.setText(dfR.format(BatteryV) + "V");
                            //电池温度
                            mTvBatteryTemperature.setText(Float.toString(Controller_data.Temperature) + "℃");
                            //外界风速
                            mTvOutSideWindSpeed.setText(Float.toString(Controller_data.OutSide_Wind_Speed) + "m/s");
                            //保存停止转速
                            mTvCommunicateState.setText("正常");

                            mTvFanVoltage.setText(Float.toString(Controller_data.Wind_V) + "V");

                            mTvfanCurrent.setText(Float.toString(Controller_data.Wind_A) + "A");

                            //发电量
                            java.math.BigDecimal bigDec2 = new java.math.BigDecimal(Controller_data.leijifadian_kWH);
                            double leijifadian_kWH1 = bigDec2.setScale(3, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
                            DecimalFormat df1 = new DecimalFormat("0.000");
                            mTvTotalPower.setText(df1.format(leijifadian_kWH1) + "kWh");
                            //逆变电压
                            mTvInverterVoltage.setText(Float.toString(Controller_data.nibian_V) + "V");
                            mTvInverterCurrent.setText(Float.toString(Controller_data.nibian_A) + "A");
                            //保留一位小数(特殊功率处理方式)
                            DecimalFormat mformat = new DecimalFormat("0.0");
                            String InverterPower = mformat.format(Controller_data.nibian_A * Controller_data.nibian_V * 1.732);
                            mTvInverterPower.setText(InverterPower + "W");

                            mNetwork_Receive_flag = 0;
                        }
                        break;
                    case 1:
                        mTvCommunicateState.setText(getResources().getString(R.string.ConnectedNetworkSuccessfully));

                        Network_Send_flag = 1;    //当不知道控制器参数时发送读取参数
                        break;

                    default:

                        break;
                }

            } catch (Exception e) {
            }
        }
    };


    public class ConnectThread extends Thread {          //---------连接线程
        //Socket mmSocket;

        public ConnectThread(Socket mmSocket) {
            //this.mmSocket = mmSocket;
        }

        public void run() {
            ct = new ConnectedThread(mmSocket);
            ct.start();

        }
    }

    public class ConnectedThread extends Thread {         //管理线程

        public ConnectedThread(Socket socket) {

            // 通过mReceiveHandler 发送 Network_Send_flag = 1;//当不知道控制器参数时发送读取参数
            Message mTimeMessage = new Message();
            mTimeMessage.what = 1;
            mReceiveHandler.sendMessage(mTimeMessage);
        }

        @Override
        public void run() {
            int bytes;
            int i_network = 0;
            //byte buff;
            while (true) {
                try {
                    byte[] buffer = new byte[128];

                    bytes = mmInStream.read(buffer);
                    //数据筛选用于排除无用 数据
                    for (i_network = 0; i_network < bytes; i_network++) {
                        if (mNetwork_Receive_flag == 0) {

                            if (buffer[i_network] == (byte) 0xeb) {
                                gbuffer[0] = (byte) 0xeb;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else if (mNetwork_Receive_flag == 1) {
                            if (buffer[i_network] == (byte) 0x90) {
                                gbuffer[1] = (byte) 0x90;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else if (mNetwork_Receive_flag == 2) {
                            if (buffer[i_network] == (byte) 0xeb) {
                                gbuffer[2] = (byte) 0xeb;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else if (mNetwork_Receive_flag == 3) {
                            if (buffer[i_network] == (byte) 0x90) {
                                gbuffer[3] = (byte) 0x90;
                                mNetwork_Receive_flag++;
                            } else {
                                mNetwork_Receive_flag = 0;
                            }

                        } else {
                            if (mNetwork_Receive_flag < 42)               //接收
                            {
                                gbuffer[mNetwork_Receive_flag] = buffer[i_network];
                                mNetwork_Receive_flag++;
                            }
                            if (mNetwork_Receive_flag > 41 && mNetwork_Receive_flag != 50) {
                                int sum_i = 0;
                                int sumh = 0;
                                for (sum_i = 4; sum_i < 40; sum_i++) {
                                    sumh += (gbuffer[sum_i] & 0x000000ff);
                                }
                                if ((gbuffer[40] == (byte) ((sumh & 0x0000ff00) >> 8)) && (gbuffer[41] == (byte) (sumh & 0x000000ff))) {
                                    //已通过校验为合法数据帧
                                    //表示接收完成
                                    mNetwork_Receive_flag = 50;
                                } else
                                    mNetwork_Receive_flag = 0;
                            }

                        }
                    }
                    //用mReceiveHandler接受网络发回的数据
                    mReceiveHandler.obtainMessage(801, bytes, -1, gbuffer).sendToTarget();

                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main Activity to shutdown the connection */
        // 关闭连接
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
            }
        }
    }

    public class NetworkThread extends Thread {              //此线程用于实现系统1S一次的定时作用


        public void run() {
            while (true) {

                try {
                    if (Network_Send_flag == 1)                              //当不知道控制器参数时发送读取参数
                        Network_Send((byte) 0x01, (byte) 0x00);
                    else if (Network_Send_flag == 2)                             //读取控制器数据
                        Network_Send((byte) 0x00, (byte) 0x00);
                    else if (Network_Send_flag == 3)                             //恢复参数
                        Network_Send((byte) 0x03, (byte) 0x00);
                    else if (Network_Send_flag == 4)                             //向控制器中写参数
                        Network_Send((byte) 0x04, (byte) 0x00);
                    Thread.sleep(3000);


                } catch (InterruptedException e) {
                    break;
                }

            }
        }
    }


    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }


    // 用于存放控制器的数据，
    public class Controller_Data {
        public boolean Run_state = false;
        public boolean Hand_Stop = false;                 //手动刹车标志
        public boolean Overload_Speed = false;            //过速刹车标志
        public boolean Unload_Load = false;               //卸载标志

        public boolean Battery_L = false;                //蓄电池欠压标志
        public boolean Battery_H = false;                    //蓄电池过压标志

        public boolean Load_Overload = false;                //负载过载
        public boolean Load_ShortCircuit = false;            //负载短路

        public boolean Load1_On = false;                    //负载1有输出
        public boolean Load2_On = false;                    //负载2有输出
        public boolean Tempreture_Over = false;             //温度过高

        public float Battery_V = 0;                         //电池的电压
        public float Battery_A = 0;                         //电池的电流
        public float Battery_W = 0;                         //电池的功率
        public float Wind_V = 0;                            //风能的电压
        public float Wind_A = 0;                            //风能的电流
        public float Wind_W = 0;                            //风能的功率
        public float Wind_Speed = 0;                        //风机转速

        public float Sun_V = 0;                             //光能电压
        public float Sun_A = 0;                             //光能电流
        public float Sun_W = 0;                             //光能功率

        public float Load1_A = 0;                              //负载一电流
        public float Load2_A = 0;                              //负载二电流

        public float Temperature = 0;                           //温度
        public float Win_Speed = 0;                             //风速
        public double KVdata = 0;                               //电压系数
        public double KIdata = 0;                               //电流系数
        public double KPdata = 0;                               //电流系数

        public float nibian_A = 0;                             //逆变电流
        public float nibian_V = 0;                             //逆变电压
        public double leijifadian_kWH = 0;                      //累计发电量
        public float OutSide_Wind_Speed = 0;                        // 外界风速
        public float Fan_Stop_Speed = 0;                            //风机停止转速；
        public String Fan_unloading_voltage = "";                    //风机卸载电压点
        public String Fan_unloading_current = "";                    //风机卸载电流点
        public String Fan_Begin_Charge_Voltage = "";                  //风机起始电压点
        public String Unloading_voltage = "";                          //卸载电压点
        public String Day_Break_Voltage = "";                          //天亮检测点
        public String Short_Voltage = "";                       //欠压点
        public String Out_Execessive_Voltage = "";              //输出电压点
        public String Day_Dark_Voltage = "";                    //天黑电压点
        public String Short_Voltage_Recover = "";               //欠压恢复点
        public int mHalfTimeStartTime1;
        public int mTimeOffTime1;
        public int mHalfTimeStartTime2;
        public int mTimeOffTime2;


        //将gbuffer中的数据填充到数据寄存器 调用此方法后系统数据得到更新
        public void Buf_Produce() {                 //解析收到的数据包

            //可以在这个方法中判断是接收到的什么数据

            if ((gbuffer[5] & 0xff) == 0x06) {     //数据应答

                Wind_V = (float) ((((gbuffer[12] & 0xff) << 1) + ((gbuffer[13] & 0x80) >> 7)) * KVdata);
                Wind_A = (float) ((((gbuffer[22] & 0xff) << 1) + ((gbuffer[23] & 0x80) >> 7)) * KIdata);
                Wind_W = (float) ((((gbuffer[16] & 0xff) << 10) + ((gbuffer[17] & 0xff) << 2) + ((gbuffer[18] & 0xff) >> 6)) * KPdata);

                Sun_V = (float) ((((gbuffer[14] & 0xff) << 1) + ((gbuffer[15] & 0x80) >> 7)) * KVdata);
                Sun_A = (float) ((((gbuffer[24] & 0xff) << 1) + ((gbuffer[25] & 0x80) >> 7)) * KIdata);
                Sun_W = (float) ((((gbuffer[19] & 0xff) << 10) + ((gbuffer[20] & 0xff) << 2) + ((gbuffer[21] & 0xff) >> 6)) * KPdata);
                Battery_V = (float) ((((gbuffer[10] & 0xff) << 1) + ((gbuffer[11] & 0x80) >> 7)) * KVdata);
                Battery_W = (float) (Wind_W + Sun_W);
                Battery_A = (float) ((int) ((Battery_W / Battery_V) * 10)) / 10;
         /*
          * DATA19负载二电流，DATA20负载一电流，每位表示0.1A.与DATA2结合判断，有输出且电流大于0.5A,显示为绿灯和输出正常。
          * 有输出但电流小于0.5A,显示为黄灯和无负载。
          * 无输出且电流小于0.5A显示为黑灯和无输出，此时如果有过载，短路，欠压，过压，则显示红灯和故障状态；
            无输出但电流大于0.5A,显示为红灯和输出异常
          * */
                Load2_A = (float) ((gbuffer[26] & 0xff) * 0.1);
                Load1_A = (float) ((gbuffer[27] & 0xff) * 0.1);

                //新增温度/风机转速/逆变电压/逆变电流
                Temperature = (float) ((gbuffer[30] & 0xff) - 40);
                Wind_Speed = (float) (((gbuffer[28] & 0xff) << 8) + (gbuffer[29] & 0xff));
                nibian_V = (float) ((((gbuffer[38] & 0xff) << 8) + (gbuffer[39] & 0xff)) * 0.1);
                nibian_A = (float) ((((gbuffer[26] & 0xff) << 8) + (gbuffer[27] & 0xff)) * 0.1);
                leijifadian_kWH = (double) ((((gbuffer[34] & 0x000000ff) << 24) + ((gbuffer[35] & 0x0000ff) << 16) + ((gbuffer[36] & 0x00ff) << 8) + (gbuffer[37] & 0xff)) * 0.001);

                // 外界风速(待确定。。。)

                OutSide_Wind_Speed = (float) ((((gbuffer[32] & 0xff) << 8) + (gbuffer[33] & 0xff)) * 0.1);

                if ((gbuffer[8] & 0x01) != 0)                //蓄电池过压
                    Battery_H = true;
                else
                    Battery_H = false;

                if ((gbuffer[8] & 0x02) != 0)                //蓄电池欠压
                    Battery_L = true;
                else
                    Battery_L = false;

                if ((gbuffer[8] & 0x04) != 0)                //过载
                    Load_Overload = true;
                else
                    Load_Overload = false;

                if ((gbuffer[8] & 0x08) != 0)                //短路
                    Load_ShortCircuit = true;
                else
                    Load_ShortCircuit = false;

                if ((gbuffer[8] & 0x10) != 0)                //卸载
                    Unload_Load = true;
                else
                    Unload_Load = false;

                if ((gbuffer[8] & 0x20) != 0)                //过速刹车
                    Overload_Speed = true;
                else
                    Overload_Speed = false;

                if ((gbuffer[8] & 0x40) != 0)                //手动刹车
                    Hand_Stop = true;
                else
                    Hand_Stop = false;

                if ((gbuffer[9] & 0x01) != 0)                //1on
                    Load1_On = true;
                else
                    Load1_On = false;

                if ((gbuffer[9] & 0x02) != 0)                //2on
                    Load2_On = true;
                else
                    Load2_On = false;
                if (gbuffer[9] == 7) {
                    Tempreture_Over = true;

                } else {
                    Tempreture_Over = false;
                }

            } else if ((gbuffer[5] & 0xff) == 0x07)                // 参数应答
            {
                Network_Send_flag = 2;                             // 切换到读数据
                get_K(gbuffer[8]);                                 // 调用解析系统

                int i = 0;
                for (i = 0; i < 42; i++)
                    System_parameter[i] = gbuffer[i];

                //卸载电压点、、

                Fan_unloading_voltage = Float.toString((float) Math.round((System_parameter[10] & 0xff) * (float) Kvdatacs * 10) / 10);
                //卸载电流点
                Fan_unloading_current = Float.toString((float) Math.round((System_parameter[11] & 0xff) * (float) Kidatacs * 10) / 10);
                // 风机起始充电电压
                Fan_Begin_Charge_Voltage = Float.toString((float) Math.round((System_parameter[22] & 0xff) * (float) Kvdatacs * 10) / 10);
                // 风机停止转速
                Fan_Stop_Speed = (float) ((System_parameter[19] & 0xff) << 8) + ((System_parameter[20]) & 0xff);
                // 风机卸载电压点：
                //  Fan_unloading_voltage = Float.toString((System_parameter[30] & 0xff )* (float) Kvdatacs)) ;


                //导纳值
                int DNZ = System_parameter[21] & 0xff;
                //欠压点
                Short_Voltage = Float.toString((float) Math.round((System_parameter[12] & 0xff) * (float) Kvdatacs * 10) / 10);

                //输出过压点
                Out_Execessive_Voltage = Float.toString((float) Math.round((System_parameter[14] & 0xff) * (float) Kvdatacs * 10) / 10);

                //天亮检测点
                Day_Break_Voltage = Float.toString((float) Math.round((System_parameter[15] & 0xff) * (float) Kvdatacs * 10) / 10);

                //天黑检测点
                Day_Dark_Voltage = Float.toString((float) Math.round((System_parameter[16] & 0xff) * (float) Kvdatacs * 10) / 10);
                //欠压恢复点
                Short_Voltage_Recover = Float.toString((float) Math.round((System_parameter[13] & 0xff) * (float) Kvdatacs * 10) / 10);


                //Load1 一路半功率开始时间
                mHalfTimeStartTime1 = System_parameter[24] & 0xff;

                //Load1一路时空关时间
                mTimeOffTime1 = System_parameter[25] & 0xff;
                //设置Load2 二路半功率开始时间
                mHalfTimeStartTime2 = System_parameter[27] & 0xff;
                //Load2二路时空关时间
                mTimeOffTime2 = System_parameter[28] & 0xff;


                //系统参数 2 : 判断运行还是刹车
                if ((System_parameter[9] & 0x04) == 0X04) {
                    Run_state = false;
                } else {
                    Run_state = true;
                }
                if ((System_parameter[29] == 0x00)) {
                    //   Log.e("机型判断：","普通机型");
                    // 风光互补控制器
                    if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) == 0) {
                        RxBus.getInstance().post(new SettingEvent2("001", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));

                    } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) == 0) {
                        //    (System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) == 0
                        Log.e("机型判断：", "---AAAA-----纯光伏控制器--AAAA--");
                        RxBus.getInstance().post(new SettingEvent2("002", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));

                    } else if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) != 0) {
                        // 风机控制器
                        RxBus.getInstance().post(new SettingEvent2("004", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));


                    } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) != 0) {
                        // 逆变器
                        RxBus.getInstance().post(new SettingEvent2("005", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));

                    }
                } else {
                    //  特殊机型 10千瓦逆变一体机
                    Log.e("机型判断：", "风机控制逆变一体机");
                    RxBus.getInstance().post(new SettingEvent2("003", Fan_unloading_current, Fan_unloading_voltage,
                            (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                            Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));


                }

                // 保存控制器系统参数
            } else if ((gbuffer[5] & 0xff) == 0x08)                // 恢复出厂设置应答
            {
                Network_Send_flag = 2;                             // 切换到读数据
                int i = 0;
                for (i = 0; i < 42; i++)
                    System_parameter[i] = gbuffer[i];               // 保存控制器系统参数


                Fan_unloading_voltage = Float.toString((float) Math.round((System_parameter[10] & 0xff) * (float) Kvdatacs * 10) / 10);

                Fan_unloading_current = Float.toString((float) Math.round((System_parameter[11] & 0xff) * (float) Kidatacs * 10) / 10);
                Fan_Begin_Charge_Voltage = Float.toString((float) Math.round((System_parameter[22] & 0xff) * (float) Kvdatacs * 10) / 10);
                Fan_Stop_Speed = (float) ((System_parameter[19] & 0xff) << 8) + ((System_parameter[20]) & 0xff);
                // 风机卸载电压点：
                //  Fan_unloading_voltage = Float.toString((System_parameter[30] & 0xff )* (float) Kvdatacs)) ;


                //导纳值
                int DNZ = System_parameter[21] & 0xff;
                //欠压点
                Short_Voltage = Float.toString((float) Math.round((System_parameter[12] & 0xff) * (float) Kvdatacs * 10) / 10);

                //输出过压点
                Out_Execessive_Voltage = Float.toString((float) Math.round((System_parameter[14] & 0xff) * (float) Kvdatacs * 10) / 10);

                //天亮检测点
                Day_Break_Voltage = Float.toString((float) Math.round((System_parameter[15] & 0xff) * (float) Kvdatacs * 10) / 10);

                //天黑检测点
                Day_Dark_Voltage = Float.toString((float) Math.round((System_parameter[16] & 0xff) * (float) Kvdatacs * 10) / 10);
                //欠压恢复点
                Short_Voltage_Recover = Float.toString((float) Math.round((System_parameter[13] & 0xff) * (float) Kvdatacs * 10) / 10);


                //Load1 一路半功率开始时间
                mHalfTimeStartTime1 = System_parameter[24] & 0xff;

                //Load1一路时空关时间
                mTimeOffTime1 = System_parameter[25] & 0xff;
                //设置Load2 二路半功率开始时间
                mHalfTimeStartTime2 = System_parameter[27] & 0xff;
                //Load2二路时空关时间
                mTimeOffTime2 = System_parameter[28] & 0xff;
                //系统参数 2 : 判断运行还是刹车
                if ((System_parameter[9] & 0x04) == 0X04) {
                    Run_state = false;
                } else {
                    Run_state = true;
                }
                if ((System_parameter[29] == 0x00)) {
                    Log.e("机型判断：", "普通机型");
                    if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) == 0) {
                        // 风光互补控制器
                        RxBus.getInstance().post(new SettingEvent2("001", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));

                    } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) == 0) {
                        RxBus.getInstance().post(new SettingEvent2("002", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));
                    } else if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) != 0) {
                        // 风机控制器
                        RxBus.getInstance().post(new SettingEvent2("004", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));


                    } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) != 0) {
                        // 逆变器
                        RxBus.getInstance().post(new SettingEvent2("005", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));


                    }

                } else {
                    //  特殊机型 10千瓦逆变一体机
                /*    if ((gbuffer[8] & 0xf7) != 0) {   }*/
                        Log.e("机型判断：", "风机控制逆变一体机");

                        RxBus.getInstance().post(new SettingEvent2("003", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));



                }

            } else if ((gbuffer[5] & 0xff) == 0x09)                 // 写用户参数应答
            {
                Network_Send_flag = 2;                              // 切换到读数据
                int i = 0;
                for (i = 0; i < 42; i++)
                    System_parameter[i] = gbuffer[i];               // 保存控制器系统参数


                Fan_unloading_voltage = Float.toString((float) Math.round((System_parameter[10] & 0xff) * (float) Kvdatacs * 10) / 10);

                Fan_unloading_current = Float.toString((float) Math.round((System_parameter[11] & 0xff) * (float) Kidatacs * 10) / 10);
                Fan_Begin_Charge_Voltage = Float.toString((float) Math.round((System_parameter[22] & 0xff) * (float) Kvdatacs * 10) / 10);
                Fan_Stop_Speed = (float) ((System_parameter[19] & 0xff) << 8) + ((System_parameter[20]) & 0xff);
                // 风机卸载电压点：
                //  Fan_unloading_voltage = Float.toString((System_parameter[30] & 0xff )* (float) Kvdatacs)) ;


                //导纳值
                int DNZ = System_parameter[21] & 0xff;
                //欠压点
                Short_Voltage = Float.toString((float) Math.round((System_parameter[12] & 0xff) * (float) Kvdatacs * 10) / 10);

                //输出过压点
                Out_Execessive_Voltage = Float.toString((float) Math.round((System_parameter[14] & 0xff) * (float) Kvdatacs * 10) / 10);

                //天亮检测点
                Day_Break_Voltage = Float.toString((float) Math.round((System_parameter[15] & 0xff) * (float) Kvdatacs * 10) / 10);

                //天黑检测点
                Day_Dark_Voltage = Float.toString((float) Math.round((System_parameter[16] & 0xff) * (float) Kvdatacs * 10) / 10);
                //欠压恢复点
                Short_Voltage_Recover = Float.toString((float) Math.round((System_parameter[13] & 0xff) * (float) Kvdatacs * 10) / 10);


                //Load1 一路半功率开始时间
                mHalfTimeStartTime1 = System_parameter[24] & 0xff;

                //Load1一路时空关时间
                mTimeOffTime1 = System_parameter[25] & 0xff;
                //设置Load2 二路半功率开始时间
                mHalfTimeStartTime2 = System_parameter[27] & 0xff;
                //Load2二路时空关时间
                mTimeOffTime2 = System_parameter[28] & 0xff;

                //系统参数 2 : 判断运行还是刹车
                if ((System_parameter[9] & 0x04) == 0X04) {
                    Run_state = false;
                } else {
                    Run_state = true;
                }
                if ((System_parameter[29] == 0x00)) {
                    //   Log.e("机型判断：", "普通机型");

                    if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) == 0) {
                        // 风光互补控制器 001
                        RxBus.getInstance().post(new SettingEvent2("001", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));
                    } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) == 0) {
                        // 纯光控制器 002
                        RxBus.getInstance().post(new SettingEvent2("002", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));

                    } else if ((System_parameter[9] & 0x40) == 0 && (System_parameter[39] & 0x80) != 0) {
                        // 风机控制器 004
                        RxBus.getInstance().post(new SettingEvent2("004", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));


                    } else if ((System_parameter[9] & 0x40) != 0 && (System_parameter[39] & 0x80) != 0) {
                        // 逆变器 005
                        RxBus.getInstance().post(new SettingEvent2("005", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));


                    }
                } else {
                    //  特殊机型 10千瓦逆变一体机 003
                  /*  if ((gbuffer[8] & 0xf7) != 0) {   }*/
                        Log.e("机型判断：", "10kw风机控制逆变一体机");
                        RxBus.getInstance().post(new SettingEvent2("003", Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state, DNZ, Short_Voltage,
                                Out_Execessive_Voltage, Day_Break_Voltage, Day_Dark_Voltage, Short_Voltage_Recover, mHalfTimeStartTime1, mTimeOffTime1, mHalfTimeStartTime2, mTimeOffTime2));

                     /*   RxBus.getInstance().post(new SettingEvent(Fan_unloading_current, Fan_unloading_voltage,
                                (int) (Controller_data.Fan_Stop_Speed), Fan_Begin_Charge_Voltage, Run_state));*/


                }


            }
        }


        //内部方法用于解析参数从而得到电压系数电流系数功率系数KVdata,KIdata,KPdata入口参数是系统字节

        public void get_K(byte data) {
            byte xByte;
            // Log.e("data---:",data);
            xByte = (byte) (data & 0x0F);
       //    Log.e("xByte", xByte + "");
       //     Log.e("xData", data + "");
            //电压选择位
            int vSelect = 0;
            int pSelect = 0;
            if (xByte == 0) {
                vSelect = 12;
            } else if (xByte == 1) {
                vSelect = 24;
            } else if (xByte == 2) {
                vSelect = 36;
            } else if (xByte == 3) {
                vSelect = 48;
            } else if (xByte == 4) {
                vSelect = 96;
            } else if (xByte == 5) {
                vSelect = 110;
            } else if (xByte == 6) {
                vSelect = 120;
            } else if (xByte == 7) {
                vSelect = 220;
            } else if (xByte == 8) {
                vSelect = 240;
            } else if (xByte == 9) {
                vSelect = 360;
            } else if (xByte == 10) {
                vSelect = 480;
            }

            //功率选择位
            xByte = (byte) (data & 0x70);
            if (xByte == 0) {
                pSelect = 300;
            } else if (xByte == 0x10) {
                pSelect = 600;
            } else if (xByte == 0x20) {
                pSelect = 1000;
            } else if (xByte == 0x30) {
                pSelect = 2000;
            } else if (xByte == 0x40) {
                pSelect = 3000;
            } else if (xByte == 0x50) {
                pSelect = 5000;
            } else if (xByte == 0x60) {
                pSelect = 10000;
            } else if (xByte == 0x70) {
                pSelect = 20000;
            }

            //得到电压系数
            switch (vSelect) {
                case 12:
                    KVdata = 0.05;
                    Kvdatacs = 0.1;

                    break;
                case 24:
                    KVdata = 0.1;
                    Kvdatacs = 0.2;
                    break;
                case 36:
                    KVdata = 0.2;
                    Kvdatacs = 0.4;       // 0.3改为0.4
                    break;
                case 48:
                    KVdata = 0.2;
                    Kvdatacs = 0.4;
                    break;
                case 96:
                    KVdata = 0.5;
                    Kvdatacs = 1;       // 0.8改为1
                    break;
                case 110:
                    KVdata = 0.5;
                    Kvdatacs = 1;
                    break;
                case 120:
                    KVdata = 0.5;
                    Kvdatacs = 1;
                    break;
                case 220:
                    KVdata = 1;
                    Kvdatacs = 2;
                    break;
                case 240:
                    KVdata = 1;
                    Kvdatacs = 2;
                    break;
                case 360:
                    KVdata = 2;
                    Kvdatacs = 4;         //3 改为4
                    break;
                case 480:
                    KVdata = 2;
                    Kvdatacs = 4;
                    break;
                default:
                    KVdata = 1;
            }
            //得到电流系数
            if ((vSelect == 24 && pSelect == 1000)
                    || (vSelect == 48 && pSelect == 2000)
                    || (vSelect == 240 && pSelect == 10000)
                    || (vSelect == 480 && pSelect == 20000)
                    || (vSelect == 120 && pSelect == 5000)) {
                KIdata = 0.2;
                Kidatacs = 0.4;
            } else if ((vSelect == 48 && pSelect == 3000)
                    || (vSelect == 48 && pSelect == 5000)
                    || (vSelect == 120 && pSelect == 10000)
                    || (vSelect == 240 && pSelect == 20000)) {
                KIdata = 0.4;
                Kidatacs = 0.8;
            } else {
                KIdata = 0.1;
                Kidatacs = 0.2;
            }
            //得到功率系数
            if ((pSelect == 600 && vSelect == 24)) {
                KPdata = 0.01;
            } else if ((pSelect == 1000 && vSelect == 24)) {
                KPdata = 0.01 * 2;
            } else if ((pSelect == 2000 && vSelect == 48)) {
                KPdata = 0.01 * 2 * (48 / 24);
            } else if ((pSelect == 20000 && vSelect == 480)) {
                KPdata = 0.01 * 2 * (480 / 24);
            } else if ((pSelect == 10000 && vSelect == 240)) {
                KPdata = 0.01 * 2 * (240 / 24);
            } else if ((pSelect == 5000 && vSelect == 120)) {
                KPdata = 0.01 * 2 * (120 / 24);
            } else if ((pSelect == 3000 && vSelect == 48)) {
                KPdata = 0.01 * 4 * (48 / 24);
            } else if ((pSelect == 5000 && vSelect == 48)) {
                KPdata = 0.01 * 4 * (48 / 24);
            } else if ((pSelect == 10000 && vSelect == 120)) {
                KPdata = 0.01 * 4 * (120 / 24);
            } else if ((pSelect == 20000 && vSelect == 240)) {
                KPdata = 0.01 * 4 * (240 / 24);
            } else {
                KPdata = vSelect / 24.0 * 0.01;
            }
        }

    }

    @Override
    public void onDestroyView() {
        Network_Send_flag = 0;
        if (ct != null) {
            ct.cancel();
        }
        super.onDestroyView();

    }


    // 以屏幕宽度适配（返回值是true）
    @Override
    public boolean isBaseOnWidth() {
        return true;
    }

    @Override
    public float getSizeInDp() {
        return 670;
    }

}
